type objFun = {
    [key:string]:Function
}
type funArr = Array<objFun>
class Bus{
    eventArr:funArr = []
    $on(onevent:string, cb:Function) {
        //判断数组中是否存在改监听函数
        let test = this.eventArr.find(item => item[onevent])
        //如果存在，则覆盖
        if (test) {
            test[onevent] = cb
        } else {
            //否则创建一个新的监听函数
            let obj = Object.create(null)
            obj[onevent] = cb
            this.eventArr.push(obj)
        }
    }
    $emit(emitevent:string, msg?:any) {
        //每次发布的时候遍历数组，执行对应的监听函数
        this.eventArr.find(item => item[emitevent])?.[emitevent](msg)
    }
    $off(emitevent:string){
        let eventIndex = this.eventArr.findIndex(item => item[emitevent])
        eventIndex&&this.eventArr.splice(eventIndex,1)
    }
}

export default new Bus